package LeetCode.leetcode46;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<Integer> path = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        backTracking(path,res,nums,new boolean[nums.length]);
        return res;
    }
    public static void backTracking(List<Integer> path,List<List<Integer>> res,int[] nums,boolean[] used){
        if (path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i]) continue;
            path.add(nums[i]);
            used[i] = true;
            backTracking(path,res,nums,used);
            path.remove(path.size()-1);
            used[i] = false;
        }
    }
}
